class ReporterImagesController < ApplicationController
  before_filter :authenticate
  layout "reporter"
  
  # GET /images
  # GET /images.xml
  def index
    raise Exceptions::UserAccessError if !current_user.isRole?(Role.find_by_alias('REPORTER'))
    
    @filter = params[:filter].nil? ? {} : params[:filter]
    @images = Image.reporter_images_list(current_user, @filter).paginate :page=>params[:page], :order=>'created_at desc', :per_page => 5
	@events = Event.all
	@image_status = ImageStatus.all
	
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @images }
    end
  end

  # GET /images/1
  # GET /images/1.xml
  def show
    raise Exceptions::UserAccessError if !current_user.isRole?(Role.find_by_alias('REPORTER'))
    
    @image = Image.find(params[:id])
	@event = Event.all
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @image }
    end
  end

  # GET /images/new
  # GET /images/new.xml
  def new
    raise Exceptions::UserAccessError if !current_user.isRole?(Role.find_by_alias('REPORTER'))
    
    @image = Image.new
    @events = Event.all.collect {|p| [ p.title, p.id ] }
    
    @thumbnail = Thumbnail.all
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @image }
    end
  end

  # GET /images/1/edit
  def edit
    raise Exceptions::UserAccessError if !current_user.isRole?(Role.find_by_alias('REPORTER'))
    
    @image = Image.find(params[:id])
    @events = Event.all.collect {|p| [ p.title, p.id ] }
	@event = Event.all
	@thumbnail = Thumbnail.all
  end

  # POST /images
  # POST /images.xml
  def create
    raise Exceptions::UserAccessError if !current_user.isRole?(Role.find_by_alias('REPORTER'))
    
    @image = Image.new(params[:image])
	
    respond_to do |format|
      if @image.save
        pic = params[:image][:picture]
		#Update @image.picture 
		@bienid=@image.id
     @image.title = pic.original_filename if @image.title.empty? || @image.title.nil?
		@image.picture =@bienid.to_s + "_" + pic.original_filename
		
		@image.width=ImageTools.getwidth(@file_path)
		@image.height = ImageTools.getheight(@file_path)
		#@file = pic
		#ImageTools.createsizeimage(@file)
		@image.save
		
		
		#Thumbnail image
		@ids = params[:ids]
		@ids.each do |object|
			@a = Thumbnail.find(object[0])
			if File.file?(@file_path)
            # create thumbnail
            ImageTools.createThumbnail(@file_path, "public/images/event/thumbnails/#{@image.id.to_s + "_" + @a.height + "x" + @a.width + "_" + pic.original_filename}", @a.height.to_i, @a.width.to_i)
			else
            @image.destroy
			end
		end
		# 
        format.html { redirect_to(@image, :notice => 'Image was successfully created.') }
        
        
        format.xml  { render :xml => @image, :status => :created, :location => @image }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @image.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /images/1
  # PUT /images/1.xml
  def update
    raise Exceptions::UserAccessError if !current_user.isRole?(Role.find_by_alias('REPORTER'))
    

    @image = Image.find(params[:id])

    respond_to do |format|
      if @image.update_attributes(params[:image])
        format.html { redirect_to(url_for(:action => 'index'), :notice => 'Image was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @image.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  def save
    raise Exceptions::UserAccessError if !current_user.isRole?(Role.find_by_alias('REPORTER'))
    
    @images = params[:images]
    
    @images.each do |item|
      
      if !item[:picture].nil?
        @image = Image.new(item)
        
        # Add event id
        @image.event_id = params[:image][:event_id]
        
        # add current user
        @image.user = current_user
        
        pic = item[:picture]
        
        #attributes
        @image.title = pic.original_filename if @image.title.empty? || @image.title.nil?
        @image.picture = pic.original_filename
        
        @image.save
        @image.upload(pic)
        @image.createThumbnails
      end    
      
    end
    
    respond_to do |format|

      format.html { redirect_to(url_for(:action => 'index'), :notice => 'Images was successfully added.') }
      format.xml  { head :ok }
 
    end

  end
 
	

  # DELETE /images/1
  def destroy
    raise Exceptions::UserAccessError if !current_user.isRole?(Role.find_by_alias('REPORTER'))
    
	@image = Image.find(params[:id])
	@image.deleteImage
	@image.destroy
	
	
	respond_to do |format|
          format.html { redirect_to(url_for(:action => 'index')) }
          format.xml  { head :ok }
        end
  end

  
  def upload
    raise Exceptions::UserAccessError if !current_user.isRole?(Role.find_by_alias('REPORTER'))
    
    @image = Image.new(params[:image])
    @event = Event.find(@image.event_id)           
    @image.event = @event
    
    pic = params[:image][:picture]
    @image.picture = pic.original_filename
    @image.title = pic.original_filename if @image.title.empty? || @image.title.nil?
    
 
    respond_to do |format|
      if @image.save          
          
          # Upload picture    
          @image.upload(pic)
          @image.createThumbnails
          
          format.html { render :layout => false }
         else
          format.html { render :layout => false }
      end
    end
    

  end
  
  def recreate_thumb
    @image = Image.find(params[:id])
    
    @image.deleteThumbnails
    @image.createThumbnails
    
    respond_to do |format|
        format.html { redirect_to({:controller => 'reporter_images'}, :notice => "Thumbnails was successfully recreated.") }
        format.xml  { head :ok }
        format.js { render 'destroy.js.erb' }
    end
  end
  
  def delete_selected
    raise Exceptions::UserAccessError if !current_user.isRole?(Role.find_by_alias('REPORTER'))
    
    @ids = params[:ids]
    @user = current_user
    
      @ids.each do |obj|
      @image = Image.find(obj[0])
          @image.deleteImage
          @image.destroy          
      end
    
    
    respond_to do |format|
        format.html { redirect_to({:action => 'index'}, :notice => "Images was successfully deleted.") }
        format.xml  { head :ok }
        format.js { render 'destroy.js.erb' }
    end
  end
  
  def small_gallery
    render :layout => false
  end
end
